package top.lbqaq.service;

import top.lbqaq.dao.BaseDao;
import top.lbqaq.dao.MaterialDao;
import top.lbqaq.dao.SupplierDao;
import top.lbqaq.domain.PageBean;
import top.lbqaq.error.NullObjectException;
import top.lbqaq.pojo.Material;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author yzj
 * @Date 2021/11/22
 */
public class MaterialService {
    private final MaterialDao materialDao = new MaterialDao();
    private final SupplierDao supplierDao = new SupplierDao();
    public PageBean<Material> selectAllMaterial(String currentPage1,String rows1) {
        Connection connection = BaseDao.getConnection();
        PageBean<Material> pb = new PageBean<>();
        int currentPage = Integer.parseInt(currentPage1);
        int rows = Integer.parseInt(rows1);
        pb.setCurrentPage(currentPage);
        pb.setRows(rows);
        int totalCount = 0;
        try {
            totalCount = materialDao.getAllMaterialNumber(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        pb.setTotalCount(totalCount);
        int start = (currentPage-1)*rows;
        List<Material> list= null;
        try {
            list = materialDao.getAllMaterial(connection,start,rows);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        pb.setList(list);
        int totalPage = (totalCount%rows) ==0?(totalCount/rows):(totalCount/rows+1);
        pb.setTotalPage(totalPage);
        return pb;
    }

    public PageBean<Material> selectByCategory(String mCategory,String currentPage1,String rows1) {
        Connection connection = BaseDao.getConnection();
        PageBean<Material> pb = new PageBean<>();
        int currentPage = Integer.parseInt(currentPage1);
        int rows = Integer.parseInt(rows1);
        pb.setCurrentPage(currentPage);
        pb.setRows(rows);
        int totalCount = 0;
        try {
            totalCount = materialDao.getMaterialNumberByCategory(connection,mCategory);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        pb.setTotalCount(totalCount);
        int start = (currentPage-1)*rows;
        List<Material> list= null;
        try {
            list = materialDao.getMaterialByCategory(connection,start,rows,mCategory);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        pb.setList(list);
        int totalPage = (totalCount%rows) ==0?(totalCount/rows):(totalCount/rows+1);
        pb.setTotalPage(totalPage);
        return pb;
    }

    public Material selectByName1(String name) {
        Connection connection = BaseDao.getConnection();
        Material material = null;
        try {
            material = materialDao.getMaterialByName1(connection,name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return material;
    }

    public PageBean<Material> selectByName(String mName,String currentPage1,String rows1) {
        Connection connection = BaseDao.getConnection();
        PageBean<Material> pb = new PageBean<>();
        int currentPage = Integer.parseInt(currentPage1);
        int rows = Integer.parseInt(rows1);
        pb.setCurrentPage(currentPage);
        pb.setRows(rows);
        int totalCount = 0;
        try {
            totalCount = materialDao.getMaterialNumberByName(connection,mName);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        pb.setTotalCount(totalCount);
        int start = (currentPage-1)*rows;
        List<Material> list= null;
        try {
            list = materialDao.getMaterialByName(connection,start,rows,mName);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        pb.setList(list);
        int totalPage = (totalCount%rows) ==0?(totalCount/rows):(totalCount/rows+1);
        pb.setTotalPage(totalPage);
        return pb;
    }

    public PageBean<Material> selectInByName1(String mName,String currentPage1,String rows1) {
        Connection connection = BaseDao.getConnection();
        PageBean<Material> pb = new PageBean<>();
        int currentPage = Integer.parseInt(currentPage1);
        int rows = Integer.parseInt(rows1);
        pb.setCurrentPage(currentPage);
        pb.setRows(rows);
        int totalCount = 0;
        try {
            totalCount = materialDao.getMaterialInNumberByName(connection,mName);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        pb.setTotalCount(totalCount);
        int start = (currentPage-1)*rows;
        List<Material> list= null;
        try {
            list = materialDao.getMaterialInByName1(connection,start,rows,mName);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        pb.setList(list);
        int totalPage = (totalCount%rows) ==0?(totalCount/rows):(totalCount/rows+1);
        pb.setTotalPage(totalPage);
        return pb;
    }

    public List<Material> selectInByName(String mName) {
        Connection connection = BaseDao.getConnection();
        try {
            List<Material> allMaterial = materialDao.getMaterialInByName(mName, connection);
            if (allMaterial.isEmpty()) {
                return null;
            } else {
                return allMaterial;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    public int insertMaterial(Material material) {
        Connection connection = BaseDao.getConnection();
        if (material.getFlag() == 1) {
            try {
               Material material1= materialDao.getMaterialByName1(connection,material.getMaterialName());
               if(material1.getMaterialName()==null){
                   return  materialDao.insertMaterial(material, connection);
               }
               else{
                   long a=material.getMaterialAmount()+material1.getMaterialAmount();
                   material.setMaterialAmount(a);
                   return  materialDao.updateByName(material,connection);
               }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        else if(material.getFlag()==0){
            Material material1= null;
            try {
                material1 = materialDao.getMaterialByName1(connection,material.getMaterialName());
            } catch (SQLException e) {
                e.printStackTrace();
            }
            if(material1.getMaterialName()==null){
                try {
                    return  materialDao.insertMaterial(material, connection);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            else{
                return -1;
            }
        }
        return 0;
    }

    public int updateByName(Material material) {
        Connection connection = BaseDao.getConnection();
        try {
            return materialDao.updateByName(material, connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public int deleteByName(String name){
        Connection connection = BaseDao.getConnection();
        return materialDao.deleteByName(name,connection);

    }

    public int materialin(Material material) {
        Connection connection = BaseDao.getConnection();
        int i = 0;
        try {
            if(supplierDao.queryBySname(connection,material.getSupplyCompany()).isEmpty()){
                i=  materialDao.materialin(material,connection);
                throw new NullObjectException("系统无此供货单位记录，请先添加供货单位信息");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        i=  materialDao.materialin(material,connection);
        return i ;
    }
}
